<template>
  <div>
    <el-dialog
      title="新增模板"
      :visible="visible"
      width="40%"
      @close="visible = false"
      :close-on-click-modal="false"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-width="100px"
        class="demo-form"
        label-position="right"
        size="mini"
        style="width:80%"
        v-loading="loading"
      >
        <el-form-item label="封面">
          <el-upload
            class="avatar-uploader"
            :show-file-list="false"
            :action="$store.getters['app/resourceUploadUrl']"
            :on-success="onCoverUploadSuccessHandler"
          >
            <img
              v-if="form.cover.fileId"
              :src="form.cover.fileId|getResourceUrl"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="名称">
          <el-input type="text" v-model="form.name"></el-input>
        </el-form-item>
        <br>
        <el-form-item label="描述">
          <el-input type="textarea" v-model="form.description"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="confirm" size="small"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import resourceApi from "@/api/resource";
export default {
  filters:{
     getResourceUrl(fileId) {
      return resourceApi.getResourceUrl(fileId);
    },
  },
  props: {
    value: {
      id: 1
    }
  },
  watch: {
    value: {
      handler() {
        this.load();
      },
      deep: true
    },
    visible(newVal, oldVal) {
      if (!newVal && oldVal) this.hide();
    }
  },
  computed:{
    
  },
  model: {
    prop: "value",
    event: "change"
  },
  data() {
    return {
      loading: false,
      visible: false,
      defaultLabelWidth: "150px",
      form: {
        id: "",
        name: "",
        cover: {
          fileId: "",
          fileName: ""
        },
        description: ""
      },
      rules: {
        name: [{ required: true, message: "请输入", trigger: "change" }]
      }
    };
  },
  methods: {
    confirm() {
      this.commit();
    },
    show(value) {
      this.visible = true;
      if (value) this.loadData(value);
    },
    hide() {
      this.visible = false;
      this.form = {
        id: "",
        name: "",
        cover: {
          fileId: "",
          fileName: ""
        },
        description: ""
      };
    },
    loadData(value) {
      this.loading = true;
      Object.keys(value).forEach(key => {
        this.form[key] = value[key];
      });
      
      this.loading = false;
    },
    commit() {
      const _this = this;
      this.form.screenId=this.form.id
      this.$refs["form"].validate(valid => {
        if (valid) {
          debugger;
          this.$store.dispatch("screen/updateBaseInfo", this.form).then(res => {
            this.$emit("ok-click", res);
            this.hide();
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
   
    onCoverUploadSuccessHandler(response, file, fileList) {
      debugger;
      let fileItem = response.data[0];
      if (!this.form.cover) this.$set(this.form, "cover", {});
      this.$set(this.form.cover, "fileId", fileItem.id);
      this.$set(this.form.cover, "fileName", fileItem.fileName);
    }
  }
};
</script>

<style></style>
